package com.rpframework.website.device.act.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.rpframework.module.adminbase.domain.AdminUser;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.GoodsStock;
import com.rpframework.website.device.service.GoodsStockService;

/**
 * 库存表
 * @author zhangli
 * @date 2016年3月31日 下午4:08:31
 */
@Controller
@RequestMapping("/admin/stock")
public class AdminGoodsStockAct extends AdminAct{
	@Resource GoodsStockService service;
	
	/**
	 * 库存列表
	 * @param pager
	 * @param state
	 * @param session
	 * @param model
	 * @return
	 * @time 2016年3月31日 下午4:09:09
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, 
			@RequestParam(value="state", required=false) Integer state, 
			@RequestParam(value="goodsId", required=false) String goodsId, 
			@RequestParam(value="relativeId", required=false) String relativeId, 
			@RequestParam(value="remind", required=false) String remind, 
			HttpSession session,
			Map<Object, Object> model){
		if(pager == null) {
			pager = new Pager();
		}
		AdminUser t = (AdminUser) session.getAttribute(SESSION_ADMIN_USER_KEY);
		if(t.getAdminRole().getIsSuper() != 1){
			pager.getSearchMap().put("relativeId", t.getRelativeId().toString());
		}
		if(StringUtils.isNotBlank(remind)){
			//在警示列表中  要分出每条数据的操作状态     去配送   配送中  已配送
			
			pager.getSearchMap().put("remind", remind);
			pager = service.getPager(pager);
			model.put("pager", pager);
			return this.doPackageURI("goods_stock/remind");
		}
		//relativeId 为0就是平台的库存 其它对应各个物业的库存
		if(StringUtils.isNotBlank(relativeId)) pager.getSearchMap().put("relativeId", relativeId);
		model.put("adminRole", t.getAdminRole().getIsSuper());
		model.put("relativeId", t.getRelativeId().toString());
		if(NumberUtils.isValid(state)){
			pager.getSearchMap().put("state", state.toString());
			model.put("state", state);
		}
		if(StringUtils.isNotBlank(goodsId)){
			pager.getSearchMap().put("goodsId", goodsId);
			//单独查出当前商品的总数
			Double amounts = service.getGoodsStockAmount(Integer.valueOf(goodsId));
			model.put("amounts", amounts);
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("goods_stock/list");
	}
	
	@RequestMapping("/on")
	public @ResponseBody JsonElement onState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	@RequestMapping("/off")
	public @ResponseBody JsonElement offState(@RequestParam(value="id", required=false) Integer id){
		JsonObject json = new JsonObject();
		boolean flag = service.doUpdateState(id);
		if(flag){
			json.addProperty("succ", true);
		}else{
			json.addProperty("succ", false);
		}
		return json;
	}
	
	@RequestMapping("/delete-{id}")
	public String delete(@PathVariable Integer id ,RedirectAttributes attr){
		boolean flag = service.delete(id);
		if(flag){
			setInfoMsg("删除成功！", attr);
		}else{
			setInfoMsg("删除失败！", attr);
		}
		return redirect("/admin/goods_stock/list");
	}
	@RequestMapping("/{id}/detail")
	public String detail(@PathVariable Integer id,RedirectAttributes attr, Map<Object, Object> model,HttpSession session){
		GoodsStock t = service.select(id);
		Assert.notNull(t,"Not find Id: " + id);
		model.put("entity", t);
		return this.doPackageURI("goods_stock/detail");
	}
}
